import indexTpl from '../views/index.art'
import img from '../assets/user2-160x160.jpg'
import pageHeader from '../components/pageheader'
import page from '../databus/page'

const index = (router) => {
  return (req, res, next) => {
    const html = indexTpl(
      {
        subRouter: res.subRoute(),
        img
      }
    )
    next(html)

    const socket = io.connect('http://localhost:3000')

    socket.on('message', function(msg) {
      if(msg === 'ok') {
        let num = ~~$('#icon-email').text()
        $('#icon-email').text(++num)
      }
    })

    // res.render(html)

    // window resize 让页面撑满（在页面加载完之后）
    $(window, '.wrapper').resize()

    // 加载页面导航
    pageHeader()

    const $lis = $('#sidebar-menu li:not(:first-child)')

    $lis.on('click', function() {
      const url = $(this).attr('to')
      router.go(url) // 如果不用go跳转可以使用a链接跳转 href="#/index/users"
    })

    let hash = location.hash.slice(1)
    $lis.filter(`li[to="${hash}"]`).addClass('active').siblings().removeClass('active')

    // 是否重置页码
    if(location.hash !== page.curRoute) {
      page.reset()
    }
    // 当前url保存
    page.setCurRoute(location.hash)


    
    // 登出路由绑定 
    $('#users-signout').on('click', function(e) {
      e.preventDefault()
      localStorage.setItem('lg-token', '')
      location.reload()
    })
  }
}
export default index
